@extends('admin.admin')

@section('content')
    <link rel="stylesheet" href="{{ asset("/components/AdminLTE/plugins/daterangepicker/daterangepicker-bs3.css") }}">
    <link rel="stylesheet" href="{{ asset("/css/admin/coupon.css") }}">

<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        优惠券管理
        <small>优惠券列表</small>
        <small>编辑</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 控制面板</a></li>
        <li >优惠券管理</li>
        <li ><a href="/admin/coupon">优惠券列表</a></li>
        <li class="active">编辑</li>
    </ol>
</section>
<?php

Form::macro(
        'selectRequired',
        function($name, $options = [], $selected = null, $attributes = [], $disabled = [])
        {
            $html = '<select name="' . $name . '"';
            foreach ($attributes as $attribute => $value)
            {
                $html .= ' ' . $attribute . '="' . $value . '"';
            }
            $html .= '>';

            foreach ($options as $value => $text)
            {
                $html .= '<option value="' . $value . '"' .
                        ($value == $selected ? ' selected="selected"' : '') .
                        (in_array($value, $disabled) ? ' disabled="disabled"' : '') . '>' .
                        $text . '</option>';
            }

            $html .= '</select>';

            return $html;
        }
);?>

<!-- Main content -->
<section class="content">

    <div class="row">
        <div class="col-xs-12">
            <div class="box box-primary">
                {!! Form::model($coupons,['route'=>['admin.coupon.update',$coupons->id],'method' => 'post','class'=>'form-horizontal']) !!}

                <input type="hidden" id="use_id" name="use_id" value="1346"><!-- 开发测试用，要去掉 -->

                <div class="box-header with-border">
                    <h3 class="box-title">优惠券编辑</h3>
                    <div class="box-tools">
                        <a href="{{Route('admin.coupon.index')}}" type="button" class="btn btn-block btn-success btn-xs"><i class="icon  glyphicon glyphicon glyphicon-menu-left"></i> 返回</a>
                    </div>
                </div>

                <div class="box-body ">

                    <?php $select = $category::getTreeList();?>

                    <?php

                    function myspace($n,$hasChild){
                        $enum = ['├','└'];
                        if ($hasChild){
                            $space = $enum[0];
                        }else{
                            $space = $enum[1];
                        }
                        $str = '';
                        for ($i=0;$i<=$n;$i++){
                            $str.='&nbsp;&nbsp;';
                        }
                        return $str.$space;
                    }
                    $seletcItem[0] = '所有商品分类';

                    $disabledOption = [];
                    foreach ($select as $k=> $item){

                        if($item->has_children){
                            $disabledOption[]=$item->cat_id;
                        }
                        $seletcItem [$item->cat_id]= myspace($item->deepth,$item->has_children).$item->cat_name;
                    }

                    ?>
                    <div class="nav-tabs-custom">
                        <ul class="nav nav-tabs" id="nav">
                            <li class="active"><a href="#base" data-toggle="tab">基本信息</a></li>
                        </ul>

                        <div class="tab-content">

                            <div class="active tab-pane" id="base">

                                <div class="form-group @if($errors->has('name')) has-error   @endif">
                                    <label class="col-sm-2 control-label" for="exampleInputPassword1">名称</label>
                                    <div class="col-sm-3">
                                        {!! Form::text('name',old('name'),array('class'=>'form-control','placeholder'=>'优惠券名称'))!!}
                                        @if($errors->has('name'))
                                            <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('name')}}</label>
                                        @endif
                                    </div>
                                </div>

                                <div class="form-group @if($errors->has('value')) has-error   @endif">
                                    <label for="exampleInputPassword1" class="col-sm-2 control-label">优惠券金额</label>
                                    <div class="col-sm-3">
                                        {!! Form::text('value',old('value'),array('class'=>'form-control','placeholder'=>'优惠券金额'))!!}

                                        @if($errors->has('value'))
                                            <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('value')}}</label>
                                        @endif
                                    </div>
                                </div>

                                <div class="form-group @if($errors->has('amount')) has-error   @endif">
                                    <label for="exampleInputPassword1" class="col-sm-2 control-label">发放数量</label>
                                    <div class="col-sm-3">
                                        {!! Form::text('amount',old('amount'),array('class'=>'form-control','placeholder'=>'发放数量'))!!}

                                        @if($errors->has('amount'))
                                            <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('amount')}}</label>
                                        @endif
                                    </div>
                                </div>

                                <div class="form-group @if($errors->has('type')) has-error   @endif">
                                    <label for="exampleInputPassword1" class="col-sm-2 control-label">可用类型</label>
                                    <div class="J-type col-sm-3">
                                        {!! Form::select('type',$typeArr,old('type'),['class'=>'form-control', 'id'=>'J-proType' , 'required'])!!}

                                        @if($errors->has('type'))
                                            <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('type')}}</label>
                                        @endif
                                    </div>
                                </div>

                                <div class="J-selWrap hide form-group @if($errors->has('use_id_str')) has-error   @endif">
                                    <label class="J-label col-sm-2 control-label">适用范围</label>
                                    <div class="J-type col-sm-3">

                                        {!! Form::select('',$typeArr,old('type'),['data-type'=>@$coupons->type , 'data-selected'=>@$use_id_str , 'data-placeholder'=>'请选择' , 'class'=>'J-sel2 form-control select2 select2-hidden-accessible-', 'multiple'])!!}
                                        @if($errors->has('use_id_str'))
                                            <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('use_id_str')}}</label>
                                        @endif
                                    </div>
                                </div>
                                <input id="selectResult" class="sr-only" type="text" name="use_id_str" value="{{@$use_id_str}}">

                                <div class="J-resultList @if($coupons->type!=2)hide @endif form-group @if($errors->has('use_id_str')) has-error   @endif">
                                    <label class="col-sm-2 control-label">选择商品</label>
                                    <div class="col-sm-3">
                                        <div class="add-goods">
                                            <!-- <button type="button" class="form-control" data-toggle="modal" data-target="#GoodsModal">请选择商品</button> -->
                                        </div>
                                            {!! Form::button('请选择商品',['class'=>'form-control', 'id'=>'J-proType', 'data-toggle'=>'modal', 'data-target'=>'#GoodsModal', 'required'])!!}
                                            @if($errors->has('use_id_str'))
                                                <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('use_id_str')}}</label>
                                            @endif
                                        <div class="goods-list  @if($coupons->type!=2)hide @endif" style="width: 800px;">
                                            <table class="table table-bordered">

                                                <thead>
                                                <tr>
                                                    <th style="width: 50px">ID</th>
                                                    <th style="width: 50px">货号</th>
                                                    <th>名称</th>
                                                    <th style="width: 150px">分类</th>
                                                    <th style="width: 80px">价格</th>
                                                    <th style="width: 80px">库存</th>
                                                    <th style="width: 80px">类型</th>
                                                    <th style="width: 80px">审核状态</th>
                                                    <th style="width: 80px">上下架</th>
                                                </tr>
                                                </thead>
                                                <tbody>

                                                @if(!empty(@$goods))
                                                    @foreach($goods as $item)
                                                    <tr id="selGoods-{{@$item['goods_id']}}" class="J-tr" data-id="{{@$item['goods_id']}}">

                                                        <td>{{@$item['goods_id']}}</td>
                                                        <td>{{@$item['goods_sn']}}</td>
                                                        <td>
                                                            <a class="J-linkG" href="/goods/{{$item['goods_id']}}?proview=1" target="_blank">{{@$item['goods_name']}}</a>
                                                        </td>
                                                        <td>
                                                            <?php
                                                                $categorys = $category::find(@$item['cat_id']);
                                                                echo $categorys->cat_name;
                                                            ?>
                                                        </td>
                                                        <td>{{@$item['shop_price']}}</td>
                                                        <td>{{@$item['goods_number']}}</td>
                                                        <td>
                                                            @if(@$item['type']==0)
                                                                一般商品
                                                            @elseif(@$item['type']==1)
                                                                e贸易商品
                                                            @endif
                                                        </td>
                                                        <td>
                                                            <div class="label @if(@$item['audit_status']==0)label-warning @elseif(@$item['audit_status']==1) label-success @elseif(@$item['audit_status']==2) label-warning @endif">

                                                                <?php
                                                                    switch (@$item['audit_status']) {
                                                                        case '0':
                                                                            echo '未审核';
                                                                            break;
                                                                        case '1':
                                                                            echo '审核通过';
                                                                            break;
                                                                        case '2':
                                                                            echo '审核不通过';
                                                                            break;
                                                                    }
                                                                ?>
                                                            </div>
                                                        </td>
                                                        <td>
                                                            <div class="label @if(@$item['is_on_sale']==1) label-success @elseif(@$item['is_on_sale']==2)label-warning @endif">
                                                                <?php
                                                                    switch (@$item['is_on_sale']) {
                                                                        case '1':
                                                                            echo '上架';
                                                                            break;
                                                                        case '2':
                                                                            echo '下架';
                                                                            break;
                                                                    }
                                                                ?>
                                                            </div>
                                                        </td>
                                                    </tr>
                                                    @endforeach
                                                @endif
                                                </tbody>
                                            </table>
                                        </div>

                                        @if($errors->has('goods'))
                                            <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('goods')}}</label>
                                        @endif
                                    </div>
                                </div>


                                <div class="form-group @if($errors->has('limit_fir')) has-error   @endif">
                                    <label for="exampleInputPassword1" class="col-sm-2 control-label">优惠券使用满减规则</label>
                                    <div class="col-sm-3">
                                        {!! Form::text('limit_fir',old('limit_fir'),array('class'=>'form-control','placeholder'=>'优惠券使用满减规则'))!!}

                                        @if($errors->has('limit_fir'))
                                            <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('limit_fir')}}</label>
                                        @endif
                                    </div>
                                </div>

                                <div class="form-group @if($errors->has('remarks')) has-error   @endif">
                                    <label for="exampleInputPassword1" class="col-sm-2 control-label">优惠券备注说明</label>
                                    <div class="col-sm-3">
                                        {!! Form::text('remarks',old('remarks'),array('class'=>'form-control','placeholder'=>'优惠券备注说明'))!!}

                                        @if($errors->has('remarks'))
                                            <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('remarks')}}</label>
                                        @endif
                                    </div>
                                </div>

                                <div class="form-group @if($errors->has('start_time') || $errors->has('end_time')) has-error   @endif">
                                    <label for="exampleInputPassword1" class="col-sm-2 control-label">优惠券时间</label>
                                    <div class="col-sm-2">
                                        {!! Form::text('start_time',old('start_time'),array('class'=>'form-control daterange1','placeholder'=>'开始时间'))!!}

                                        @if($errors->has('start_time'))
                                            <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('start_time')}}</label>
                                        @endif
                                    </div>
                                    <div style="float:left;width:10px;height:34px;line-height:34px;">到</div>
                                    <div class="col-sm-2">
                                        {!! Form::text('end_time',old('end_time'),array('class'=>'form-control daterange1','placeholder'=>'结束时间'))!!}

                                        @if($errors->has('end_time'))
                                            <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('end_time')}}</label>
                                        @endif
                                    </div>
                                </div>

                                <!-- <div class="form-group @if($errors->has('coupon_thumb')) has-error   @endif">
                                    <label for="exampleInputPassword1" class="col-sm-2 control-label">优惠券缩略图</label>

                                    <div class="col-sm-3">
                                        <div style="width: 170px;">
                                            <div class="box  box-default image-uploader">
                                                <style>
                                                    .upload-btn{
                                                        display: block; width:48px;height:30px;position: absolute;filter:alpha(opacity=0);-moz-opacity:0;-khtml-opacity: 0;opacity: 0;
                                                    }
                                                </style>
                                                /.box-header
                                                <div class="box-body">

                                                    <div style="width: 145px;height: 145px; display: block;">
                                                        <img class="image" style="width: 145px;; padding: 1px;" width="145" height="145" src="@if (old('image_url')) {{old('image_url')}} @else http://placehold.it/800x800 @endif"/>
                                                        <input type="hidden" id="image_url" name="image_url" value="{{old('image_url')}}">
                                                    </div>
                                                    <div class="message" style="width: 145px;height: 145px; display: block;position: absolute; padding: 5px; top:0px; color: red; text-align: center; line-height: 145px;display: none;">

                                                    </div>

                                                    <div class="progress progress-xxs" style="position: absolute;width: 140px; left:13px; display: none;">
                                                        <div class="progress-bar progress-bar-default " role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 0%">
                                                        </div>
                                                    </div>
                                                </div>
                                                /.box-body
                                                <div class="box-footer" style="position: relative">

                                                    <button class="btn btn-sm btn-default btn-flat pull-left" type="button">上传</button>
                                                    {!! Form::file('file',['class'=>'upload-btn','id'=>'fileupload','type'=>'file','data-url'=>'/admin/file_upload']) !!}
                                                    {!! Form::hidden('coupon_thumb',old('coupon_thumb'),['id'=>'thumb']) !!}

                                                    <button class="btn  btn-sm btn-danger btn-flat pull-right" type="button">删除</button>
                                                </div>
                                            </div>
                                            /.box
                                        </div>

                                    </div>

                                    @if($errors->has('coupon_thumb'))
                                        <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('coupon_thumb')}}</label>
                                    @endif
                                </div> -->

                                <div class="form-group ">
                                    <label for="weight" class="col-sm-2 control-label"></label>

                                    <div class="col-sm-3">
                                        <button type="submit"  class="btn btn-primary">保存</button>

                                    </div>
                                </div>

                            </div>

                        </div>
                        <!-- /.tab-content -->
                    </div>

                </div>
                <!-- /.box-body -->
                <div class="box-footer">
                    <input type="hidden" name="pre_url" value="@if(empty($back_url)){{route('admin.coupon.index')}}@else{{@$back_url}}@endif"/>

                    {!! Form::hidden('_method', 'PUT') !!}


                </div>
                {!! Form::close() !!}


                <div class="J-onLoading overlay hide">
                    <i class="fa fa-refresh fa-spin"></i>
                </div>

            </div>
            <!-- /.box -->
        </div>
    </div>

</section>
<!-- /.content -->

<div id="GoodsModal" class="modal fade">
    <div class="modal-dialog  modal-lg">
        <div class="modal-content box">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">选择商品</h4>
            </div>


            <div class="modal-body">
                <form class="J-search form-search form-inline" action="/admin/goods/1" method="get" >

                    <div class="form-group">
                        <label class="sr-only">商品分类</label>
                        <!-- <select name="cat_id" class="J-select form-control select2 select2-hidden-accessible" data-placeholder="请选择">
                            <option></option>
                            <option value="1">美妆个护</option>
                            <option value="2">进口美食</option>
                            <option value="3">服饰鞋包</option>
                        </select> -->
                        {!! Form::selectRequired('cat_id',$seletcItem,@$_GET['cat_id'],['class'=>'J-select form-control select2 select2-hidden-accessible','id'=>'cat_id','data-placeholder'=>'请选择商品分类' ],$disabledOption)!!}
                    </div>
                    <div class="form-group">
                        <label class="sr-only">品牌</label>
                        <!-- <select name="brand_id" class="J-select form-control select2 select2-hidden-accessible" data-placeholder="请选择">
                            <option></option>
                            <option value="1">阿芙妮</option>
                            <option value="2">纳爱斯</option>
                            <option value="3">绿萝</option>
                        </select> -->
                        {!! Form::select('brand_id',$brandArr,@$_GET['brand_id'],['class'=>'J-select form-control select2 select2-hidden-accessible','data-placeholder'=>'请选择品牌'])!!}
                    </div>
                    <div class="form-group">
                        <label class="sr-only">类型</label>
                        <select name="type" class="form-control">
                            <option value="">选择类型</option>
                            <option value="0">一般商品</option>
                            <option value="1">E贸易商品</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label class="sr-only">审核状态</label>
                        <select name="audit_status" class="form-control ">
                            <option value="">所有审核状态</option>
                            <option value="0">未审核</option>
                            <option value="1">审核通过</option>
                            <option value="2">审核不通过</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label class="sr-only">上下架</label>
                        <select name="is_on_sale" class="form-control">
                            <option value="">所有上下架</option>
                            <option value="1">上架</option>
                            <option value="2">下架</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label class="sr-only">商品名称</label>
                        <input type="text" name="goods_name" class="form-control" placeholder="请输入商品名称">
                    </div>

                    <div class="form-group">
                        <label class="sr-only">商品编码</label>
                        <input type="text" name="goods_sn" class="form-control" placeholder="请输入商品编码">
                    </div>

                    <button type="submit" class="J-searchBtn btn btn-primary">搜索</button>


                </form>

                <div class="box-body table-responsive" style="padding: 30px 0 0 0;">

                    <table class="table table-hover">
                        <thead>
                        <tr>
                            <th class="text-center" style="width: 60px">选择</th>
                            <th style="width: 50px">ID</th>
                            <th style="width: 50px">货号</th>
                            <th>名称</th>
                            <th style="width: 150px">分类</th>
                            <th style="width: 80px">价格</th>
                            <th style="width: 80px">库存</th>
                            <th style="width: 80px">类型</th>
                            <th style="width: 80px">审核状态</th>
                            <th style="width: 80px">上下架</th>
                        </tr>
                        </thead>
                        <tbody class="J-goodsList"></tbody>

                    </table>
                </div>
                <div class="box-footer">
                    <!-- 分页 -->
                    <div class="J-page pull-right no-margin-pager">
                        <ul class="pagination"></ul>
                    </div>
                </div>
            </div>


            <div class="J-footer modal-footer hide">
                <div class="J-result content text-left" style="min-height: 50px;"></div>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="J-ok btn btn-primary">确定</button>
            </div>

            <div class="J-loading overlay">
                <i class="fa fa-refresh fa-spin"></i>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<template id="goodsTrTpl">
    <tr id="goods-list-tr${goods_id}" class="J-tr" data-id="${goods_id}">
        <td class="checkbox-wrap text-center">
            <input class="J-check" type="checkbox" value="${goods_id}" ${is_checked}>
        </td>
        <td>${goods_id}</td>
        <td>${goods_sn}</td>
        <td>
            <a class="J-linkG" href="/goods/${goods_id}?proview=1" target="_blank">${goods_name}</a>
        </td>
        <td>${cat_name}</td>
        <td>${shop_price}</td>
        <td>${goods_number}</td>
        <td>${type}</td>
        <td>
            <div class="label label-${audit_class}">${audit_status}</div>
        </td>
        <td>
            <div class="label label-${sale_class}">${is_on_sale}</div>
        </td>
    </tr>
</template>


<script src="{{ asset ("/components/blueimp-file-upload/js/vendor/jquery.ui.widget.js") }}"></script>
<script src="{{ asset ("/components/blueimp-file-upload/js/jquery.iframe-transport.js") }}"></script>
<script src="{{ asset ("/components/blueimp-file-upload/js/jquery.fileupload.js") }}"></script>
<!-- Select2 -->
<script type="text/javascript" src="{{ asset("/components/AdminLTE/plugins/select2/select2.full.min.js") }}"></script>
<script src="{{ asset("/components/AdminLTE/plugins/daterangepicker/moment.min.js") }}"></script>
<script src="{{ asset("/components/AdminLTE/plugins/daterangepicker/daterangepicker.js") }}"></script>


<script src="{{ asset("/js/tools.js") }}"></script>
<script src="{{ asset("/js/searchGoods.js") }}"></script>
<script>
    //Initialize Select2 Elements
    $(".select2").select2();

    $(function () {
        $('.image-uploader').each(function(k,imageUploader){
            var wrapper = $(imageUploader);//盒子
            var uploadBtn = wrapper.find('#fileupload');
            var imageFile = wrapper.find('.image');
            var progress = wrapper.find('.progress');
            var progressBar = progress.find('.progress-bar');
            var inputFile = wrapper.find('#thumb');
            var imageUrl = wrapper.find('#image_url');
            var message = wrapper.find('.message');

            uploadBtn.click(function(){
                progressBar.css('width', 0 + '%');
                imageFile.attr('src','http://placehold.it/800x800');
                inputFile.val('');
                message.hide();

            });
            wrapper.find('.btn-danger').click(function(){
                progressBar.css('width', 0 + '%');
                imageFile.attr('src','http://placehold.it/800x800');
                inputFile.val('');
                message.hide();

            });

            uploadBtn.fileupload({
                dataType: 'json',
                headers:{'X-CSRF-TOKEN':'{{csrf_token()}}'},
                add: function (e, data) {
                    data.submit();
                },
                progressall: function (e, data) {
                    progress.fadeIn();
                    var percent = parseInt(data.loaded / data.total * 100, 10);
                    if (progress==1){
                        progress.show()
                    }
                    progressBar.css('width', percent + '%');
                    setTimeout(function () {
                        progress.fadeOut();
                    }, 500);
                },
                done: function (e, data) {
                    imageFile.attr('src',data.result.url);
                    inputFile.val(data.result.id);
                    imageUrl.val(data.result.url);
                },
                complete:function(){
                }
            });
        });
    });

    $('.daterange1').daterangepicker({
        showDropdowns: true,
        timePicker: true,
        timePickerIncrement: 10,
        timePicker12Hour:false,
        drops:'up',
        format: 'YYYY-MM-DD HH:mm:ss',
        singleDatePicker:true,
        locale: {
            applyLabel: '确定',
            cancelLabel: '取消',
            fromLabel: '开始',
            toLabel: '结束',
            weekLabel: '周'
        }
    });

</script>

@endsection